package fr.umlv.android.boggle;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class GameEndedActivity extends Activity {
	
	private LinkedList<String> names = new LinkedList<String>();
	private LinkedList<Integer> scores = new LinkedList<Integer>();
	private int newLine;
	private int score;
	private int player;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.game_ended);
        
        
        loadScore();
        System.out.println("taile: "+ names.size());

        System.out.println("taile: "+ scores.get(3));

        
        printWords();
        integrateScore("Olivier", score);
        printScores();
	}
	
	private void printWords() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        ArrayList<String> wordsFound = bundle.getStringArrayList("wordsFound");
        score = bundle.getInt("score");
        TextView viewScore = (TextView) findViewById(R.id.gameEndScoreView);
        viewScore.setText(String.valueOf(score));
        LinearLayout layoutWords = (LinearLayout) findViewById(R.id.gameEndWordsFound);
        for(int i=0; i<wordsFound.size(); i++) {
        	String word = wordsFound.get(i);
        	 TextView textView = new TextView(GameEndedActivity.this);
        	 textView.setText(word);
        	 layoutWords.addView(textView);
        }	
	}

	private void integrateScore(String name, int value) {
		LinkedList<String> names = new LinkedList<String>();
		LinkedList<Integer> scores = new LinkedList<Integer>();
		boolean isAdd = false;
		
		
		for(int i=0; i<10 && i<this.names.size(); i++) {

			if(isAdd == false && value > this.scores.get(i)) {
				names.add(name);
				scores.add(value);
				isAdd=true;
				newLine=i;
			}
			names.add(this.names.get(i));
			scores.add(this.scores.get(i));
		}	
	
		if (isAdd==false && names.size()<10) {
			newLine = names.size();
			names.add(name);
			scores.add(value);
		}
		
		this.names = names;
		this.scores = scores;
	}
	
	
	
	private void loadScore() {
        Resources res = getResources();
        InputStream inputStreamDico = res.openRawResource(R.raw.score);
		InputStreamReader ipsr = new InputStreamReader(inputStreamDico);
		BufferedReader br = new BufferedReader(ipsr);
		String ligne;
		
		int i=0;
		try {
			while ((ligne=br.readLine())!=null){
				if(i%2 == 0) {
					names.add(ligne);
				}
				else if(i%2 == 1) {
					scores.add(Integer.valueOf(ligne));
				}

				i++;
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
	private void printScores() {
		TableLayout grid = (TableLayout) findViewById(R.id.gameEndScoresTable);
		int size = scores.size();
		int i=0;
	
		for(; i<10 && i<size; i++) {
			
			TextView textView = new TextView(this);
			TableRow row = (TableRow) grid.getChildAt(i*2);
			
		
			textView.setText(names.get(i));
			textView.setPadding(3, 3, 3, 3);
			textView.setGravity(Gravity.CENTER);
			
	
			TextView textView2 = new TextView(this);
			textView2.setText(""+scores.get(i));
			textView2.setPadding(3, 3, 3, 3);
			textView2.setGravity(Gravity.RIGHT);
			
			if(i==newLine) {
				textView.setTextColor(Color.RED);
				textView2.setTextColor(Color.RED);
			}
			
			row.addView(textView);
			row.addView(textView2);
			
		}

	}
}
